﻿using System;
using System.Windows.Forms;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.HYSpace.SpaceManager.Forms;
using YArchitech.HYSpace.SpaceManager.Manager;

namespace YArchitech.HYSpace
{
	[Transaction(TransactionMode.Manual)]
	public class ZoneCommand : IExternalCommand
	{
		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			Result result;
			try
			{
				Transaction transaction = new Transaction(commandData.Application.ActiveUIDocument.Document, "Document");
				transaction.Start();
				DataManager dataManager = new DataManager(commandData);
				DialogResult dialogResult;
				using (SpaceManagerForm spaceManagerForm = new SpaceManagerForm(commandData.Application, dataManager))
				{
					dialogResult = spaceManagerForm.ShowDialog();
				}
				if (dialogResult == DialogResult.OK)
				{
					transaction.Commit();
					result = Autodesk.Revit.UI.Result.Succeeded;
				}
				else
				{
					transaction.RollBack();
					result = Autodesk.Revit.UI.Result.Cancelled;
				}
			}
			catch (Exception ex)
			{
				message = ex.Message;
				result = Autodesk.Revit.UI.Result.Failed;
			}
			return result;
		}
	}
}
